import { Component, OnInit, TemplateRef } from '@angular/core';
import { MemberService } from '../../services/member.service';
import { SearchMember, Member, ClassMember } from '../Interfaces/Member';
import { Page } from '../Interfaces/Page';
import { BsModalRef, BsModalService } from 'ngx-bootstrap';
import { Search, ClassEntity } from '../Interfaces/ClassEntity';
import { ClassService } from '../../services/class.service';
import { TermService } from '../../services/term.service';
import { Term } from '../Interfaces/Term';
import { ArrangmentService } from '../../services/arrangment.service';

@Component({
  selector: 'app-members',
  templateUrl: './members.component.html',
  styleUrls: ['./members.component.css']
})
export class MembersComponent implements OnInit {

    members: Member[];
    member: Member = new Member();
    page: Page = new Page();
    search: SearchMember = new SearchMember();

    rotate = false;
    maxSize = 10;

    editMemberModal: BsModalRef;

    resetMemberModal: BsModalRef;
    // 设置选中的是第几项
    selected_index: number;

    member_term_id: number;
    member_class_id: number;
    member_start_time: number;
    member_end_time: number;
    allClassEntities: ClassEntity[];
    allTerms: Term[];

    constructor(
        private memberService: MemberService,
        private modalService: BsModalService,
        private classService: ClassService,
        private termService: TermService,
        private arrangmentService: ArrangmentService
    ) { }

    ngOnInit() {
        this.getMembers(this.search);
    }

    getMembers(condition: SearchMember) {
        this.memberService.getMembersByCondition(condition).subscribe( res => {
            this.members = res.data.data as Member[];
            this.page.currentPage = res.data.current_page;
            this.page.total = res.data.total;
            this.page.perPage = res.data.per_page;
            if (this.selected_index >= 0) {
                this.member = this.members[this.selected_index];
            }
        });
    }

    searchMembers() {
        if (!this.search) {
            return false;
        } else {
            this.search.page = 1;
           this.getMembers(this.search);
        }
    }

    removeMember(item: Member) {
        if (confirm('确认删除么?')) {
            this.memberService.removeMember(item).subscribe(res => {
                if (!res.errors) {
                    alert('删除成功');
                    this.getMembers(this.search);
                } else {
                    alert('删除失败！');
                }
            });
        }
    }


    // 分页
    pageChanged(event) {
        console.log('Page changed to: ' + event.page);
        console.log('Number items per page: ' + event.itemsPerPage);
        this.search.page = event.page;
        this.getMembers(this.search);
    }


    openEditMembersModal(template: TemplateRef<any>, item) {
        console.log(item);
        this.member = JSON.parse(JSON.stringify(item));
        this.editMemberModal = this.modalService.show(template);
    }

    closeEditMemberModal() {
        this.editMemberModal.hide();
        this.member = new Member();
    }


    editMemberItem() {
        this.memberService.editMember(this.member).subscribe(
            res => {
                if (!res.errors) {
                    alert('编辑成功');
                    this.getMembers(this.search);
                    this.closeEditMemberModal();
                } else {
                    alert('编辑失败！');
                }
            }
        );
    }

    // 重新排班
    openResetMembersModal(template: TemplateRef<any>, item: Member, index: number) {
        this.member = item;
        this.selected_index = index;
        this.resetMemberModal = this.modalService.show(template,
            Object.assign({}, {}, { class: 'gray modal-lg' })
        );
        this.getAllTerms();
    }

    getAllTerms() {
        this.termService.getTerms().then(res => {
            this.allTerms = res as Term[];
        });
    }

    closeResetMemberModal () {
        this.resetMemberModal.hide();
        this.member = new Member();
        this.member_class_id = 0;
        this.member_term_id = 0;
    }


    geAllClassEntitiesByTerm() {
        // 默认显示所有班级
        const conditions = new Search();
        // conditions.term_id  = this.member_term_id;
        if (this.member_term_id) {
            conditions.term_id  = this.member_term_id;
        }
        if (this.member_start_time) {
            conditions.start_time = this.member_start_time;
        }
        if (this.member_end_time) {
            conditions.end_time = this.member_end_time;
        }
        conditions.all = true;
        this.classService.getClassEntitiesByCondition(conditions).subscribe(res => {
            this.allClassEntities  = res.data as ClassEntity[];
        });
    }

    resetMemberItem() {
        const classMember = new ClassMember();
        classMember.class_id = this.member_class_id;
        classMember.member_id = this.member.id;
        classMember.member_name = this.member.name;
        this.arrangmentService.addClassMember(classMember).subscribe(res => {
            if (!res.errors) { alert('成员加入班级成功');
            this.getMembers(this.search);
            // this.resetSelectedMember();
        } else { alert('添加失败！' + res.message); }
          });
    }

    removeMemberFromClass(classMember: ClassMember) {
        this.arrangmentService.removeMemberFromClass(classMember).subscribe(
            res => {
                if (!res.errors) {
                    alert('移除成功');
                    this.getMembers(this.search);
                } else { alert('移除失败！'); }
            }
        );
    }

}
